package com.thinkgem.jeesite.modules.hydms.students.utils;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.UnavailableSecurityManagerException;
import org.apache.shiro.session.InvalidSessionException;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

import com.thinkgem.jeesite.common.service.BaseService;
import com.thinkgem.jeesite.common.utils.CacheUtils;
import com.thinkgem.jeesite.common.utils.SpringContextHolder;
import com.thinkgem.jeesite.modules.hydms.students.dao.DmsStudentsDao;
import com.thinkgem.jeesite.modules.hydms.students.entity.DmsStudents;
import com.thinkgem.jeesite.modules.sys.security.SystemAuthorizingRealm.Principal;


public class DmsStudentsUtils {
	
		private static DmsStudentsDao studentDao = SpringContextHolder.getBean(DmsStudentsDao.class);
	
		public static final String DmsStudents_CACHE = "DmsStudentsCache";
		public static final String DmsStudents_CACHE_ID_ = "id_";
		
/**
		 * 根据ID获取用户
		 * @param id
		 * @return 取不到返回null
		 *//*
		public static DmsStudents get(String id){
			DmsStudents DmsStudents = (DmsStudents)CacheUtils.get(DmsStudents_CACHE, DmsStudents_CACHE_ID_ + id);
			if (DmsStudents ==  null){
				DmsStudents = DmsStudentsDao.get(id);
				if (DmsStudents == null){
					return null;
				}
				
				
			}
			return DmsStudents;
		}*/
		
		/**
		 * 根据登录名获取用户
		 * @param loginName
		 * @return 取不到返回null
		 *//*
		public static DmsStudents getByLoginName(String loginName){
			DmsStudents DmsStudents = (DmsStudents)CacheUtils.get(DmsStudents_CACHE, DmsStudents_CACHE_LOGIN_NAME_ + loginName);
			if (DmsStudents == null){
				DmsStudents = DmsStudentsDao.getByLoginName(new DmsStudents(null, loginName));
				if (DmsStudents == null){
					return null;
				}
				DmsStudents.setRoleList(roleDao.findList(new Role(DmsStudents)));
				CacheUtils.put(DmsStudents_CACHE, DmsStudents_CACHE_ID_ + DmsStudents.getId(), DmsStudents);
				CacheUtils.put(DmsStudents_CACHE, DmsStudents_CACHE_LOGIN_NAME_ + DmsStudents.getLoginName(), DmsStudents);
			}
			return DmsStudents;
		}
		*/
		/**
		 * 清除当前用户缓存
		 *//*
		public static void clearCache(){
			
			DmsStudentsUtils.clearCache(getDmsStudents());
		}*/
		/*
		*//**
		 * 清除指定用户缓存
		 * @param DmsStudents
		 *//*
		public static void clearCache(DmsStudents DmsStudents){
			CacheUtils.remove(DmsStudents_CACHE, DmsStudents_CACHE_ID_ + DmsStudents.getId());
			CacheUtils.remove(DmsStudents_CACHE, DmsStudents_CACHE_LOGIN_NAME_ + DmsStudents.getLoginName());
			CacheUtils.remove(DmsStudents_CACHE, DmsStudents_CACHE_LOGIN_NAME_ + DmsStudents.getOldLoginName());
			if (DmsStudents.getOffice() != null && DmsStudents.getOffice().getId() != null){
				CacheUtils.remove(DmsStudents_CACHE, DmsStudents_CACHE_LIST_BY_OFFICE_ID_ + DmsStudents.getOffice().getId());
			}
		}
		*/
		/**
		 * 获取当前用户
		 * @return 取不到返回 new DmsStudents()
		 */
		/*public static DmsStudents getDmsStudents(){
			Principal principal = getPrincipal();
			if (principal!=null){
				DmsStudents DmsStudents = get(principal.getId());
				if (DmsStudents != null){
					return DmsStudents;
				}
				return new DmsStudents();
			}
			// 如果没有登录，则返回实例化空的DmsStudents对象。
			return new DmsStudents();
		}*/

		
		
		
		
		
		
		/**
		 * 获取授权主要对象
		 */
		public static Subject getSubject(){
			return SecurityUtils.getSubject();
		}
		
		/**
		 * 获取当前登录者对象
		 */
		public static Principal getPrincipal(){
			try{
				Subject subject = SecurityUtils.getSubject();
				Principal principal = (Principal)subject.getPrincipal();
				if (principal != null){
					return principal;
				}
//				subject.logout();
			}catch (UnavailableSecurityManagerException e) {
				
			}catch (InvalidSessionException e){
				
			}
			return null;
		}
		
		public static Session getSession(){
			try{
				Subject subject = SecurityUtils.getSubject();
				Session session = subject.getSession(false);
				if (session == null){
					session = subject.getSession();
				}
				if (session != null){
					return session;
				}
//				subject.logout();
			}catch (InvalidSessionException e){
				
			}
			return null;
		}
		
		// ============== DmsStudents Cache ==============
		
		public static Object getCache(String key) {
			return getCache(key, null);
		}
		
		public static Object getCache(String key, Object defaultValue) {
//			Object obj = getCacheMap().get(key);
			Object obj = getSession().getAttribute(key);
			return obj==null?defaultValue:obj;
		}

		public static void putCache(String key, Object value) {
//			getCacheMap().put(key, value);
			getSession().setAttribute(key, value);
		}

		public static void removeCache(String key) {
//			getCacheMap().remove(key);
			getSession().removeAttribute(key);
		}
		
//		public static Map<String, Object> getCacheMap(){
//			Principal principal = getPrincipal();
//			if(principal!=null){
//				return principal.getCacheMap();
//			}
//			return new HashMap<String, Object>();
//		}
		
	}



